package com.ms.education.course.controller;

import com.ms.education.R;
import com.ms.education.course.entity.vo.SubjectVo;
import com.ms.education.course.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程类别 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-01-15
 */
@RestController
@RequestMapping("/course/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @Cacheable(value = "subject")
    @GetMapping("getSubject")
    public R getSubject(){
        List<SubjectVo> cateGoryTree=subjectService.getCateGoryTree();
        return R.ok().data("cateGoryTree",cateGoryTree);
    }

    @PutMapping("saveSubject")
    @CacheEvict(value = "subject",allEntries = true)
    public R saveSubject(SubjectVo subjectVo){
        subjectService.saveSubject(subjectVo);
        return R.ok();
    }

    @DeleteMapping("deleteSubjectById/{id}")
    @CacheEvict(value = "subject",allEntries = true)
    public R deleteSubjectById(@PathVariable("id")Integer id){
        subjectService.deleteSubjectById(id);
        return R.ok();
    }

    @PostMapping("updateSubject")
    @CacheEvict(value = "subject",allEntries = true)
    public R updateSubject(@RequestBody SubjectVo subjectVo){
        subjectService.updateSubject(subjectVo);
        return R.ok();
    }

    @PostMapping("updateSubjects")
    @CacheEvict(value = "subject",allEntries = true)
    public R updateSubjects(@RequestBody List<SubjectVo> subjectVos){
        subjectService.updateSubjects(subjectVos);
        return R.ok();
    }

}
